import './imports';

import Header from '../components/Header';
import NewsFrame from '../components/Iframe';
import Follow from '../components/Follow';

import { getUrlQueryValue } from '../libs/utils'
;((doc)=>{
  const oApp = doc.querySelector('#app');
  const currentNews = JSON.parse(window.localStorage.getItem('currentNews'));
  const followedList = JSON.parse(window.localStorage.getItem('followedList') || '[]');
  const init = ()=> {
      render();
      bindEvent();
  }
 function render(){
   const headerTpl = Header.tpl({
       url:getUrlQueryValue('path'),
       title:'资讯详情',
       showLeftIcon :true,
       showRightIcon :false
   });
   const NewsFrameTpl = NewsFrame.tpl(currentNews.url)
   oApp.innerHTML += (headerTpl + NewsFrameTpl + createFollowTpl());
 }
 //创建要渲染渲染star
 function createFollowTpl(){
   const isExist = followedList.find(item => item.uniquekey === currentNews.uniquekey);
   return isExist ?Follow.oFollow():Follow.unFollow();
 }

 function bindEvent(){
   Follow.bindEvent(doFollow);
 }


 function doFollow(status){
  let followedList = JSON.parse(window.localStorage.getItem('followedList') || '[]');
  if(status){
    followedList.push(currentNews);
  }else {
    followedList = followedList.filter(item => item.uniquekey !== currentNews.uniquekey);
  }
  localStorage.setItem('followedList', JSON.stringify(followedList))
 }
 init();

})(document)